package blog.service.impl;

import blog.service.thread.UserThreadLocal;
import blog.dao.mapper.CommentMapper;
import blog.dao.pojo.Comment;
import blog.dao.pojo.SysUser;
import blog.service.CommentsService;
import blog.service.SysUserService;
import blog.vo.CommentVo;
import blog.vo.Result;
import blog.vo.UserVo;
import blog.vo.params.CommentParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SysUserService sysUserService;

    // 通过文章id查找对应评论
    @Override
    public Result commentsByArticleId(Long articleId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId,articleId);
        queryWrapper.eq(Comment::getLevel,1);

        List<Comment> comments = commentMapper.selectList(queryWrapper);

        return Result.success(copyList(comments));

    }

    // 对文章创建评论
    @Override
    public Result comment(CommentParam commentParam) {
        SysUser sysUser = UserThreadLocal.get();

        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());

        Long parent = commentParam.getParent();
        if (parent == null || parent == 0) {  // 一级评论
            comment.setLevel(1);
            comment.setParentId(0L);

        }else{
            comment.setLevel(2);
            comment.setParentId(commentParam.getParent());
        }

        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);

        commentMapper.insert(comment);
        return Result.success(null);
    }


    // 找到子评论
    private List<CommentVo> findChildrenComment(Long parentId){
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId,parentId);
        queryWrapper.eq(Comment::getLevel,2);

        List<Comment> comments = commentMapper.selectList(queryWrapper);

        return copyList(comments);
    }

    public List<CommentVo> copyList(List<Comment> comments){
        ArrayList<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment:comments){
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    public CommentVo copy(Comment comment){
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);

        commentVo.setCreateDate(new DateTime(comment.getCreateDate()).toString("yyyy-MM-dd HH:mm"));

        UserVo userWho = sysUserService.findUserVoById(comment.getAuthorId());   // 评论者信息
        commentVo.setAuthor(userWho);

        if (comment.getLevel()>1){
            UserVo toUser = sysUserService.findUserVoById(comment.getToUid());   // 给谁评论的
            commentVo.setToUser(toUser);
        }

        List<CommentVo> childrenComment = findChildrenComment(comment.getAuthorId());  // 设置子评论
        commentVo.setChildrens(childrenComment);

        return commentVo;
    }
}
